<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>SegmentTree.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="SegmentTree code in Java">
<meta NAME="TITLE" CONTENT="SegmentTree code in Java">
<meta NAME="KEYWORDS" CONTENT="SegmentTree,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>SegmentTree.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "SegmentTree.java">SegmentTree.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac SegmentTree.java</span>
<span class="comment"> *  Execution:    java SegmentTree</span>
<span class="comment"> *  </span>
<span class="comment"> *  A segment tree data structure.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Arrays</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> * The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">SegmentTree</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class is an structure for efficient search of cummulative data.</span>
<span class="comment"> * It performs  Range Minimum Query and Range Sum Query in O(log(n)) time.</span>
<span class="comment"> * It can be easily customizable to support Range Max Query, Range Multiplication Query etc.</span>
<span class="comment"> * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment"> * Also it has been develop with  </span><span class="keyword">&lt;tt&gt;</span><span class="comment">LazyPropagation</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> for range updates, which means</span>
<span class="comment"> * when you perform update operations over a range, the update process affects the least nodes as possible</span>
<span class="comment"> * so that the bigger the range you want to update the less time it consumes to update it. Eventually those changes will be propagated</span>
<span class="comment"> * to the children and the whole array will be up to date.</span>
<span class="comment"> * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment"> * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment"> * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment"> * Example:</span>
<span class="comment"> * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment"> * SegmentTreeHeap st = new SegmentTreeHeap(new Integer[]{1,3,4,2,1, -2, 4});</span>
<span class="comment"> * st.update(0,3, 1)</span>
<span class="comment"> * In the above case only the node that represents the range [0,3] will be updated (and not their children) so in this case</span>
<span class="comment"> * the update task will be less than n*log(n)</span>
<span class="comment"> *</span>
<span class="comment"> * Memory usage:  O(n)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="type">@author</span><span class="comment"> Ricardo Pacheco </span>
<span class="comment"> * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">SegmentTree</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> Node</span><span class="symbol">[]</span><span class="normal"> heap</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> array</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> size</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Time-Complexity:  O(n*log(n))</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> array the Initialization array</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">SegmentTree</span><span class="symbol">(</span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> array</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">array </span><span class="symbol">=</span><span class="normal"> Arrays</span><span class="symbol">.</span><span class="function">copyOf</span><span class="symbol">(</span><span class="normal">array</span><span class="symbol">,</span><span class="normal"> array</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">//The max size of this array is about 2 * 2 ^ log2(n) + 1</span>
<span class="normal">        size </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">pow</span><span class="symbol">(</span><span class="number">2.0</span><span class="symbol">,</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">floor</span><span class="symbol">((</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">log</span><span class="symbol">((</span><span class="type">double</span><span class="symbol">)</span><span class="normal"> array</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">log</span><span class="symbol">(</span><span class="number">2.0</span><span class="symbol">))</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)));</span>
<span class="normal">        heap </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Node</span><span class="symbol">[</span><span class="normal">size</span><span class="symbol">];</span>
<span class="normal">        </span><span class="function">build</span><span class="symbol">(</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> array</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> array</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">//Initialize the Nodes of the Segment tree</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">build</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> size</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">();</span>
<span class="normal">        heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">from </span><span class="symbol">=</span><span class="normal"> from</span><span class="symbol">;</span>
<span class="normal">        heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">to </span><span class="symbol">=</span><span class="normal"> from </span><span class="symbol">+</span><span class="normal"> size </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">size </span><span class="symbol">==</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">sum </span><span class="symbol">=</span><span class="normal"> array</span><span class="symbol">[</span><span class="normal">from</span><span class="symbol">];</span>
<span class="normal">            heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">min </span><span class="symbol">=</span><span class="normal"> array</span><span class="symbol">[</span><span class="normal">from</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">//Build childs</span>
<span class="normal">            </span><span class="function">build</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> size </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">);</span>
<span class="normal">            </span><span class="function">build</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> from </span><span class="symbol">+</span><span class="normal"> size </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">,</span><span class="normal"> size </span><span class="symbol">-</span><span class="normal"> size </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">);</span>

<span class="normal">            heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">sum </span><span class="symbol">=</span><span class="normal"> heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">].</span><span class="normal">sum </span><span class="symbol">+</span><span class="normal"> heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">].</span><span class="normal">sum</span><span class="symbol">;</span>
<span class="normal">            </span><span class="comment">//min = min of the children</span>
<span class="normal">            heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">min </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">].</span><span class="normal">min</span><span class="symbol">,</span><span class="normal"> heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">].</span><span class="normal">min</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Range Sum Query</span>
<span class="comment">     *</span>
<span class="comment">     * Time-Complexity: O(log(n))</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">RSQ</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">RSQ</span><span class="symbol">(</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">RSQ</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>

<span class="normal">        </span><span class="comment">//If you did a range update that contained this node, you can infer the Sum without going down the tree</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">.</span><span class="normal">pendingVal </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">contains</span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">to </span><span class="symbol">-</span><span class="normal"> from </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">pendingVal</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">sum</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">intersects</span><span class="symbol">(</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">propagate</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> leftSum </span><span class="symbol">=</span><span class="normal"> </span><span class="function">RSQ</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> rightSum </span><span class="symbol">=</span><span class="normal"> </span><span class="function">RSQ</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">);</span>

<span class="normal">            </span><span class="keyword">return</span><span class="normal"> leftSum </span><span class="symbol">+</span><span class="normal"> rightSum</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Range Min Query</span>
<span class="comment">     * </span>
<span class="comment">     * Time-Complexity: O(log(n))</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">RMinQ</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">RMinQ</span><span class="symbol">(</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">RMinQ</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>


<span class="normal">        </span><span class="comment">//If you did a range update that contained this node, you can infer the Min value without going down the tree</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">.</span><span class="normal">pendingVal </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">contains</span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">pendingVal</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="normal">min</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">intersects</span><span class="symbol">(</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">propagate</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> leftMin </span><span class="symbol">=</span><span class="normal"> </span><span class="function">RMinQ</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> rightMin </span><span class="symbol">=</span><span class="normal"> </span><span class="function">RMinQ</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">);</span>

<span class="normal">            </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">leftMin</span><span class="symbol">,</span><span class="normal"> rightMin</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="normal">MAX_VALUE</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Range Update Operation.</span>
<span class="comment">     * With this operation you can update either one position or a range of positions with a given number.</span>
<span class="comment">     * The update operations will update the less it can to update the whole range (Lazy Propagation).</span>
<span class="comment">     * The values will be propagated lazily from top to bottom of the segment tree.</span>
<span class="comment">     * This behavior is really useful for updates on portions of the array</span>
<span class="comment">     * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment">     * Time-Complexity: O(log(n))</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> from</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> to</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> value</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">update</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> value</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">update</span><span class="symbol">(</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> value</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">update</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> value</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">//The Node of the heap tree represents a range of the array with bounds: [n.from, n.to]</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>

<span class="normal">        </span><span class="comment">/**</span>
<span class="comment">         * If the updating-range contains the portion of the current Node  We lazily update it.</span>
<span class="comment">         * This means We do NOT update each position of the vector, but update only some temporal</span>
<span class="comment">         * values into the Node; such values into the Node will be propagated down to its children only when they need to.</span>
<span class="comment">         */</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">change</span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">,</span><span class="normal"> value</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">intersects</span><span class="symbol">(</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">,</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">to</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">/**</span>
<span class="comment">             * Before keeping going down to the tree We need to propagate the</span>
<span class="comment">             * the values that have been temporally/lazily saved into this Node to its children</span>
<span class="comment">             * So that when We visit them the values  are properly updated</span>
<span class="comment">             */</span>
<span class="normal">            </span><span class="function">propagate</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>

<span class="normal">            </span><span class="function">update</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> value</span><span class="symbol">);</span>
<span class="normal">            </span><span class="function">update</span><span class="symbol">(</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> from</span><span class="symbol">,</span><span class="normal"> to</span><span class="symbol">,</span><span class="normal"> value</span><span class="symbol">);</span>

<span class="normal">            n</span><span class="symbol">.</span><span class="normal">sum </span><span class="symbol">=</span><span class="normal"> heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">].</span><span class="normal">sum </span><span class="symbol">+</span><span class="normal"> heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">].</span><span class="normal">sum</span><span class="symbol">;</span>
<span class="normal">            n</span><span class="symbol">.</span><span class="normal">min </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">].</span><span class="normal">min</span><span class="symbol">,</span><span class="normal"> heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">].</span><span class="normal">min</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">//Propagate temporal values to children</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">propagate</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> heap</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">n</span><span class="symbol">.</span><span class="normal">pendingVal </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">change</span><span class="symbol">(</span><span class="normal">heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v</span><span class="symbol">],</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">pendingVal</span><span class="symbol">);</span>
<span class="normal">            </span><span class="function">change</span><span class="symbol">(</span><span class="normal">heap</span><span class="symbol">[</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">],</span><span class="normal"> n</span><span class="symbol">.</span><span class="normal">pendingVal</span><span class="symbol">);</span>
<span class="normal">            n</span><span class="symbol">.</span><span class="normal">pendingVal </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span><span class="normal"> </span><span class="comment">//unset the pending propagation value</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">//Save the temporal values that will be propagated lazily</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">change</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> n</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> value</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        n</span><span class="symbol">.</span><span class="normal">pendingVal </span><span class="symbol">=</span><span class="normal"> value</span><span class="symbol">;</span>
<span class="normal">        n</span><span class="symbol">.</span><span class="normal">sum </span><span class="symbol">=</span><span class="normal"> n</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> value</span><span class="symbol">;</span>
<span class="normal">        n</span><span class="symbol">.</span><span class="normal">min </span><span class="symbol">=</span><span class="normal"> value</span><span class="symbol">;</span>
<span class="normal">        array</span><span class="symbol">[</span><span class="normal">n</span><span class="symbol">.</span><span class="normal">from</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> value</span><span class="symbol">;</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">//Test if the range1 contains range2</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">contains</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> from1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> from2</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> from2 </span><span class="symbol">&gt;=</span><span class="normal"> from1 </span><span class="symbol">&amp;&amp;</span><span class="normal"> to2 </span><span class="symbol">&lt;=</span><span class="normal"> to1</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">//check inclusive intersection, test if range1[from1, to1] intersects range2[from2, to2]</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">intersects</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> from1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> from2</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> to2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> from1 </span><span class="symbol">&lt;=</span><span class="normal"> from2 </span><span class="symbol">&amp;&amp;</span><span class="normal"> to1 </span><span class="symbol">&gt;=</span><span class="normal"> from2   </span><span class="comment">//  (.[..)..] or (.[...]..)</span>
<span class="normal">                </span><span class="symbol">||</span><span class="normal"> from1 </span><span class="symbol">&gt;=</span><span class="normal"> from2 </span><span class="symbol">&amp;&amp;</span><span class="normal"> from1 </span><span class="symbol">&lt;=</span><span class="normal"> to2</span><span class="symbol">;</span><span class="normal"> </span><span class="comment">// [.(..]..) or [..(..)..</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">//The Node class represents a partition range of the array.</span>
<span class="normal">    </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Node</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> sum</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> min</span><span class="symbol">;</span>
<span class="normal">        </span><span class="comment">//Here We store the value that will be propagated lazily</span>
<span class="normal">        </span><span class="usertype">Integer</span><span class="normal"> pendingVal </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> from</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> to</span><span class="symbol">;</span>

<span class="normal">        </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> to </span><span class="symbol">-</span><span class="normal"> from </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Read the following commands:</span>
<span class="comment">     * init n v     Initializes the array of size n with all v's</span>
<span class="comment">     * set a b c... Initializes the array  with [a, b, c ...]</span>
<span class="comment">     * rsq a b      Range Sum Query for the range [a, b]</span>
<span class="comment">     * rmq a b      Range Min Query for the range [a, b]</span>
<span class="comment">     * up  a b v    Update the [a,b] portion of the array with value v.</span>
<span class="comment">     * exit</span>
<span class="comment">     * </span><span class="keyword">&lt;p/&gt;</span>
<span class="comment">     * Example:</span>
<span class="comment">     * &lt;</span><span class="keyword">&lt;init</span>
<span class="normal">     * &lt;&lt;</span><span class="type">set</span><span class="normal"> </span><span class="type">1</span><span class="normal"> </span><span class="type">2</span><span class="normal"> </span><span class="type">3</span><span class="normal"> </span><span class="type">4</span><span class="normal"> </span><span class="type">5</span><span class="normal"> </span><span class="type">6</span>
<span class="normal">     * &lt;&lt;</span><span class="type">rsq</span><span class="normal"> </span><span class="type">1</span><span class="normal"> </span><span class="type">3</span>
<span class="normal">     * </span><span class="keyword">&gt;</span><span class="comment">&gt;Sum from 1 to 3 = 6</span>
<span class="comment">     * &lt;</span><span class="keyword">&lt;rmq</span><span class="normal"> </span><span class="type">1</span><span class="normal"> </span><span class="type">3</span>
<span class="normal">     * </span><span class="keyword">&gt;</span><span class="comment">&gt;Min from 1 to 3 = 1</span>
<span class="comment">     * &lt;</span><span class="keyword">&lt;input</span><span class="normal"> </span><span class="type">up</span><span class="normal"> </span><span class="type">1</span><span class="normal"> </span><span class="type">3</span>
<span class="normal">     * </span><span class="keyword">&gt;</span><span class="comment">&gt;[3,2,3,4,5,6]</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> args</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>


<span class="normal">        </span><span class="usertype">SegmentTree</span><span class="normal"> st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>

<span class="normal">        </span><span class="usertype">String</span><span class="normal"> cmd </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"cmp"</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            String</span><span class="symbol">[]</span><span class="normal"> line </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">().</span><span class="function">split</span><span class="symbol">(</span><span class="string">" "</span><span class="symbol">);</span>

<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"exit"</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">break</span><span class="symbol">;</span>

<span class="normal">            </span><span class="type">int</span><span class="normal"> arg1 </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> arg2 </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> arg3 </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>

<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                arg1 </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                arg2 </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">2</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">3</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                arg3 </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">3</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((!</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"set"</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"init"</span><span class="symbol">))</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> st </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Segment Tree not initialized"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> array</span><span class="symbol">[];</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"set"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                array </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">line</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">                </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> line</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    array</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="normal">i </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SegmentTree</span><span class="symbol">(</span><span class="normal">array</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"init"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                array </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">arg1</span><span class="symbol">];</span>
<span class="normal">                Arrays</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="normal">array</span><span class="symbol">,</span><span class="normal"> arg2</span><span class="symbol">);</span>
<span class="normal">                st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SegmentTree</span><span class="symbol">(</span><span class="normal">array</span><span class="symbol">);</span>

<span class="normal">                </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">();</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">.</span><span class="function">RSQ</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"up"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                st</span><span class="symbol">.</span><span class="function">update</span><span class="symbol">(</span><span class="normal">arg1</span><span class="symbol">,</span><span class="normal"> arg2</span><span class="symbol">,</span><span class="normal"> arg3</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">();</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">.</span><span class="function">RSQ</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"rsq"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Sum from %d to %d = %d%n"</span><span class="symbol">,</span><span class="normal"> arg1</span><span class="symbol">,</span><span class="normal"> arg2</span><span class="symbol">,</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">RSQ</span><span class="symbol">(</span><span class="normal">arg1</span><span class="symbol">,</span><span class="normal"> arg2</span><span class="symbol">));</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"rmq"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Min from %d to %d = %d%n"</span><span class="symbol">,</span><span class="normal"> arg1</span><span class="symbol">,</span><span class="normal"> arg2</span><span class="symbol">,</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">RMinQ</span><span class="symbol">(</span><span class="normal">arg1</span><span class="symbol">,</span><span class="normal"> arg2</span><span class="symbol">));</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Invalid command"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="cbracket">}</span>


<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">close</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
